package com.taotao.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.taotao.entity.Content;
import com.taotao.entity.ContentCategory;
import com.taotao.entity.PageInff;
import com.taotao.entity.item.category.EUTreeNode;
import com.taotao.service.ContentService;
import com.taotao.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * author: 王明跃
 * Create at: 18/7/10下午4:53
 */
@Controller
@RequestMapping("/content")
public class ContentController {

    @Resource
    private ContentService contentService;

    @ResponseBody
    @RequestMapping("/category/list")
    public List<EUTreeNode> categoryList(@RequestParam(value = "id", defaultValue = "0") Long id) {
        List<EUTreeNode> treeNodes = contentService.getCategoryList(id);
        return treeNodes;
    }

    @ResponseBody
    @RequestMapping("/category/create")
    public ResultVO create(Long parentId, String name) {
        ResultVO resultVO = new ResultVO();
        ContentCategory contentCategory = contentService.create(parentId, name);
        resultVO.setStatus(200)
                .setData(contentCategory);
        return resultVO;
    }

    @ResponseBody
    @RequestMapping("/category/update")
    public ResultVO update(Long id, String name) {
        ResultVO resultVO = new ResultVO();
        boolean b = contentService.update(id, name);
        if (b) {
            resultVO.setStatus(200);
        }
        return resultVO;
    }

    @ResponseBody
    @RequestMapping("/category/delete")
    public ResultVO delete(Long parentId, Long id) {
        ResultVO resultVO = new ResultVO();
        boolean b = contentService.delete(id);
        if (b) {
            resultVO.setStatus(200);
        }
        return resultVO;
    }

    @ResponseBody
    @RequestMapping("/query/list")
    public PageInff queryList(Integer categoryId, Integer page, Integer rows) {
        PageInff pageInff = new PageInff();
        Page<Object> p = PageHelper.startPage(page, rows);
        contentService.queryList();
        pageInff.setTotal(p.getTotal())
                .setRows(p.getResult());
        return pageInff;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public ResultVO delete(Long[] ids) {
        ResultVO resultVO = contentService.delete(ids);
        return resultVO;
    }

    @ResponseBody
    @RequestMapping("/save")
    public ResultVO save(Content content) {
        ResultVO resultVO = contentService.save(content);
        return resultVO;
    }

    @ResponseBody
    @RequestMapping("/edit")
    public ResultVO edit(Content content) {
        ResultVO resultVO = contentService.edit(content);
        return resultVO;
    }
}
